import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.Map;
import java.util.Map.Entry;


public class TestCountRelation {
    public static void main(String[] args) throws IOException {
        HashMap<String,Integer> map=new HashMap();
        HashMap<String, ArrayList<String>> listHashMap = new HashMap<>();
        String dir="D:\\study\\IRTool\\IRTool\\data\\relation.txt";
        String ucpath="D:\\study\\IRTool\\IRTool\\data\\lucene-release";
        BufferedReader in = new BufferedReader(new FileReader(dir));
        String thisline="";
        String key="";
        while ((thisline=in.readLine())!=null){
            String[] mapping=thisline.split("&&");
            key=mapping[2]+"&&"+mapping[3];
            if(map.get(key)!=null){
                map.put(key, map.get(key)+1);
                listHashMap.get(key).add(mapping[0]+"&&"+mapping[1]);
            }else {
                map.put(key, 1);
                ArrayList<String> ls=new ArrayList<>();
                ls.add(mapping[0]+"&&"+mapping[1]);
                listHashMap.put(key, ls);
            }
        }
        in.close();
        List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
        Collections.sort(list, new Comparator<Entry<String, Integer>>() {
            @Override
            public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
                return o2.getValue().compareTo(o1.getValue());
            }
        });
        int num1=0;
        int num2=0;
        int num3=0;
        int num4=0;
        FileWriter fileWriter = new FileWriter("D:\\study\\IRTool\\IRTool\\data\\d^0(Version320) OR d^0(Version310) OR d^0(base) OR d^0(Version303).txt");
        for (Map.Entry<String, Integer> mapping : list) {
//            System.out.println(mapping.getKey() + ":" + mapping.getValue());
            if(mapping.getKey().startsWith("version2.9.4")){
                String[] relationdir=mapping.getKey().split("&&");
                BufferedReader in1 = new BufferedReader(new FileReader(ucpath+"\\"+relationdir[0]+"\\"+relationdir[1]+".txt")); // 创建缓存区字符输入流，需要传如Reader对象
                String content="";
                String thisLine;
                while((thisLine = in1.readLine()) != null)    // 每次读取一行，直到文件结束
                    content+=thisLine;
                in1.close();
                System.out.println(mapping.getKey() + ":" + mapping.getValue()+"("+content+")");
                fileWriter.write(mapping.getKey() + ":" + mapping.getValue()+"("+content+")"+"\r\n");
                for(String classname:listHashMap.get(mapping.getKey())){
                    System.out.println("  "+classname);
                    fileWriter.write("  "+classname+"\r\n");
                }
                System.out.println("----------------------------------");
                fileWriter.write("-----------------------\r\n");
                num1+=mapping.getValue();
            }else if(mapping.getKey().startsWith("version3.1.0")){
                num2+=mapping.getValue();
            }else if(mapping.getKey().startsWith("version3.2.0")){
                num3+=mapping.getValue();
            }
        }
        fileWriter.close();
        System.out.println("2.9.4 :"+num1);
        System.out.println("3.1.0 :"+num2);
        System.out.println("3.2.0 :"+num3);
    }
}
